import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
// import Mine from '../views/Mine.vue'
// import Good from '../views/Good.vue'
// import Main from '../components/Main.vue'
// import Other from '../components/Other.vue'
// import Detail from '../views/Detail.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: {
      name: 'Home'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'), //异步，返回promise对象
    children: [
      {
        path: 'main',
        component: () => import('../components/Main.vue')
      },
      {
        path: 'other',
        component: () => import('../components/Other.vue')
      },
      {
        path: '',
        redirect: 'main'
      }
    ]
  },
  {
    path: '/mine',
    name: 'Mine',
    component: () => import('../views/Mine.vue')
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/good',
    name: 'Good',
    component: () => import('../views/Good.vue')
  },
  {
    path: '*',
    redirect: {
      path: '/home'
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('../views/Detail.vue')
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    component: () => import('../views/Detail.vue')
  },
  {
    path: '/detail',
    name: 'myDetail',  // 命名路由
    component: () => import('../views/Detail.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})
//全局路由守卫
// router.beforeEach((to, from, next) => {
//   console.log(to.fullPath)
//   var auth = ["/good", "/mine", "/detail"]//需要拦截的路由
//   //to:请求到哪去
//   //from:从哪来的请求
//   //next:是否要向后继续执行
//   if (auth.includes(to.fullPath)) {
//     console.log("验证token")
//     if (!localStorage.getItem("token")) {
//       next("/home")
//       return;
//     }
//   }
//   next();
// })
export default router
